import os
from src.settings import Settings
from src.process import ExeControl
from src.bot.robot import Robot
from src.user_data import UserData
from src.logger import Logger
from src.network.server import Server


class MyCli:
    def __init__(self):
        self.ensure_utf8()
        self.print_motd()
        self.conf = UserData()
        self.logger = Logger(self.conf)
        self.dls = ExeControl(self.conf, self.logger)
        self.bot = Robot(self.conf, self.logger, self.dls)
        self.server = Server(self.conf, self.logger)
        self.start()

    def do_event_loop(self) -> None:
        """命令行的支持"""
        while True:
            command = input()
            if command == "stop":
                self.stop()
                break
            elif command == "dlsload":
                self.reload()
            elif command == "start":
                self.dls.exeStart()
            else:
                self.dls.executeCmd(command)

    def start(self) -> None:
        """启动所有模块"""
        self.logger.start()
        self.dls.startAll()
        self.bot.start()
        self.server.start()

    def stop(self) -> None:
        """关闭所有模块"""
        self.dls.stopAll()
        self.bot.stop()

    def reload(self) -> None:
        """重载所有模块配置"""
        self.conf.reload()
        self.logger.reload()
        self.dls.reload()
        self.bot.reload()
        self.server.reload()

    @staticmethod
    def ensure_utf8() -> None:
        """控制台编码设置为utf-8"""
        if Settings.OS_PLATFORM == 'Windows':
            os.system("chcp 65001")

    @staticmethod
    def print_motd() -> None:
        """打印欢迎信息"""
        print("[GLF机器人] 当前程序版本『DLS - ws1.9』")
        print("[GLF机器人] 使用dlsload重新加载配置, start单独启动服务器")


if __name__ == '__main__':
    cli = MyCli()
    cli.do_event_loop()
